package com.yonyou.crm.sact.outwork.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.login.context.LoginContextConstant;
import com.yonyou.crm.sact.outwork.entity.OutworkRuleVO;
import com.yonyou.crm.sact.outwork.entity.RuleTimeListVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class OutworkRuleBizServiceImpl extends BaseBizServiceImpl<OutworkRuleVO> {
	
	public Map<String, Object> save(OutworkRuleVO vo) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<RuleTimeListVO> ruleTimeList = new ArrayList<RuleTimeListVO>();
		if (vo.getEnableState() == 2) {
			map.put("enableState", 2);
			ruleTimeList = transVo2RuleTimeList(vo);
			map.put("ruleTimeList", ruleTimeList);
			return map;
		}

		map.put("enableState", 1);
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("date", sdf.format(today));
		
//		先查询已有规则，再判断是否执行删除操作存在一个问题：如果连续操作，主从库同步延时问题会导致出错
		// 删除原规则
		String tenantIdStr = (String)InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		Map<String, Object> deleteParaMap = new HashMap<String, Object>();
		deleteParaMap.put("tenantId", tenantId);
		dao.deleteSql(OutworkRuleVO.class, "deleteRule", deleteParaMap);
		
		vo.setStartDate(today);
		vo = super.saveVO(vo);

		ruleTimeList = transVo2RuleTimeList(vo);
		map.put("ruleTimeList", ruleTimeList);
		return map;
	}
	
	@Override
	protected void beforeSaveVO(OutworkRuleVO vo) {
		super.beforeSaveVO(vo);

		String orgIdStr = (String)InvocationInfoProxy.getParameter(LoginContextConstant.OrgId);//所属公司
		Long orgId = Long.parseLong(orgIdStr);
		
		vo.setOrgId(orgId);
	}
	
	/*
	 * 获取指定日期的考勤规则(仅支持单个日期，未指定日期时，取当前日期)
	 */
	public Map<String, Object> getRule(Map<String, Object> paraMap) {
		String tenantIdStr = (String)InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}
		paraMap.put("tenantId", tenantId);
		
		OutworkRuleVO vo = (OutworkRuleVO)dao.selectOne(OutworkRuleVO.class, "selectRule", paraMap);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		List<RuleTimeListVO> ruleList = new ArrayList<RuleTimeListVO>();
		boolean isOpenRule = true;
		if (vo == null) {
			isOpenRule = false;
		} else {
			if (vo.getEnableState() == 2 || vo.getTimeRange().isEmpty()) {
				isOpenRule = false;
			} else {
				rsMap.put("startDate", vo.getStartDate());
				ruleList = transVo2RuleTimeList(vo);
			}
		}
		rsMap.put("isOpenRule", isOpenRule);
		rsMap.put("ruleList", ruleList);
		return rsMap;
	}
	
	private List<RuleTimeListVO> transVo2RuleTimeList(OutworkRuleVO vo) {
		List<RuleTimeListVO> rsList = new ArrayList<RuleTimeListVO>();
		// 没有设置规则，或者规则被停用，则返回空数组
		if (vo == null || vo.getEnableState() > 1) {
			return rsList;
		}

		String timeRange = vo.getTimeRange();
		if (timeRange == null || timeRange.isEmpty()) {
			return rsList;
		}
		String[] timeRangeArr = timeRange.split(",");
		for (String tr : timeRangeArr) {
			String[] trArr = tr.split("-");
			if (trArr.length == 2) {
				RuleTimeListVO listVO = new RuleTimeListVO();
				listVO.setStarttime(trArr[0]);
				listVO.setEndtime(trArr[1]);
				rsList.add(listVO);
			}
		}
		return rsList;
	}
	
	/*
	 * 获取指定日期的考勤规则列表
	 */
	public List<RuleTimeListVO> getRuleList(Map<String, Object> paraMap) {
		
		Map<String, Object> ruleMap = getRule(paraMap);
		@SuppressWarnings("unchecked")
		List<RuleTimeListVO> ruleList = (List<RuleTimeListVO>)ruleMap.get("ruleList");
		
		return ruleList;
	}
	
	
}
